package org.woosiyuan;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;

import ch.randelshofer.media.avi.AVIOutputStream;

public class Test {

	boolean mStopStream;
	
	String  flag = "Content-Length: ";
	String flag1 = "\r\n";
	
	public static void main(String[] args) throws Exception {
//		URL aURL;
//		aURL = new URL(
//				"http://192.168.1.102:81/videostream.cgi?user=admin&password=admin");
//		URLConnection conn = aURL.openConnection();
//		conn.connect();
//		for (;;) {
//			InputStream input = conn.getInputStream();
//			System.out.println("input = " + input);
//		}
		Test test = new Test();
		test.cameraStream();
	}

	
	//播放camera视频，禁止在UI线程中直接调用
		public void cameraStream() throws IOException {
			URL aURL;
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			ByteArrayInputStream byteArrayInputStream = null;
			File file = new File("C:\\a.avi");
			AVIOutputStream aviOutputStream = new AVIOutputStream(file, AVIOutputStream.VideoFormat.JPG);
//			try {
				aURL = new URL("http://192.168.1.106:81/videostream.cgi?user=admin&password=admin");
				URLConnection conn = aURL.openConnection();
				conn.connect();
				InputStream input = conn.getInputStream();
				System.out.println("input = " + input);
				if(input != null) mStopStream = false;
				int readLength = -1;
				String strDate;   //将读取的数据转化为string类型，以便判断包头
				int size = 0;
				while(!mStopStream && size<10) {
					byte[] buffer = new byte[1024];
					readLength = input.read(buffer,0,1024);//readLength 本次读取数据的长度
					if(readLength > 0) {
						strDate = new String(buffer, readLength);
						//index标记"Content-Length: "的起始位置
						//index1标记"\r\n"的位置，注意是"Content-Length: "之后的第一个位置
						int index = strDate.indexOf(flag);
						int index1 = strDate.indexOf(flag1,index);
						int streamLength = 0;
						if(index !=-1 &&index1 != -1) {
							//计算本次streamLength的长度
							streamLength = Integer.parseInt(strDate.substring(index+flag.length(), index1));
						}
						if(streamLength > 0) {
							if((index1+4) < readLength) {
								outStream.write(buffer, index1+4, readLength-index1-4);
								streamLength = streamLength - readLength+index1+4;
							}
							//将剩下读取的视频流存储到buffer1
							byte[] buffer1 = new byte[streamLength];
							int length = 0;
							while(length < streamLength) {
								length += input.read(buffer1,length,streamLength-length);
							}
							outStream.write(buffer1,0,streamLength);  // 将剩余的stream写入outStream
							byte[] data = outStream.toByteArray();
							
							BufferedImage bufferedImage = new BufferedImage(640, 320, BufferedImage.TYPE_BYTE_BINARY);
							byteArrayInputStream = new ByteArrayInputStream(data);
							bufferedImage = ImageIO.read(byteArrayInputStream);
							aviOutputStream.writeFrame(bufferedImage);
							size++;
							aviOutputStream.finish();
							outStream.reset();
						}
					}
				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//			try {
			outStream.close();
			byteArrayInputStream.close();
			
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}  // cameraStream()
	
}
